const express = require('express');
const app = require('express');
const router = app.Router();
module.exports = router;

/**
 * get http://localhost:9588/test/get?value1=aaa&value2=bbb
 */
router.get('/get', (req, res) => {
    // 入参
    console.log(req.query)
    res.send({
        code: 200,
        data: [{name: 1}, {name: 2}, {value1: req.query.value1}, {value2: req.query.value2}]
    })
})


/**
 * post表单提交 http://localhost:9588/test/post?user=aaa&password=bbb
 * http://localhost:9588/test/post?title=test&sub%5B%5D=1&sub%5B%5D=2&sub%5B%5D=3
 */
router.post('/post', (req, res) => {
    console.log(req.query)
    res.send({
        code: 200,
        data: [],
        msg: "登录账户为:" + req.query.user + ", 登录密码为:" + req.query.password
    })
})


/**
 * post postJson http://localhost:9588/test/postJson
 * json 参数 {"user": "aaa", "password": "bbb"}
 */
router.use(express.json()); // 解析POST JSON数据
router.post('/postJson', (req, res) => {
    const {user, password} = req.body;
    console.log(req.body)
    res.send({
        code: 200,
        data: [],
        msg: "登录账户为:" + user + ", 登录密码为:" + password
    })
})